<?php
namespace Home\Controller;

class MentorsController extends HomeController {

    public function index(){
    	$cate = I('get.cate');
    	$map['show']=1;
    	$train = M('TrainCate')->field('id,name')->where($map)->order('sort asc,id asc')->select();
    	$this->assign('train',$train);
    	$this->assign('cate',$cate);

    	unset($map);
    	if ($cate) {
    		$map['trainItem'] = array('like','%-'.$cate.'-%');
    	}
		$obj = M('Mentors');
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 20);
		$show = $page->show();
		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();	
		foreach ($list as $key => $value) {
            $value['trainItem'] = trainStrtoArr($value['trainItem']);
            $where['id'] = array('in',$value['trainItem']);
            $list[$key]['trainItem'] = M('TrainCate')->field('name')->where($where)->select();
		}	
		$this->assign('list', $list);
		$this->assign('page', $show);
    	$this->display();
    }

    public function detail(){
    	$id = I('get.id');
    	$list = M('Mentors')->where($map)->find();
    	if (!$list) {
    		$this->error('The information does not exist');
    	}

        $list['trainItem'] = trainStrtoArr($list['trainItem']);
        $where['id'] = array('in',$list['trainItem']);
        $list['trainItem'] = M('TrainCate')->field('name')->where($where)->select();

    	$this->assign('list',$list);
    	$this->display();
    }

    public function submit(){
        if ($this->loginStatus) {
            if ($this->userType==2) {
                $this->error('You are the mentors can submit'); 
            }else{
                //完善资料
                $list = M('Mentees')->where('userid='.$this->userID)->find();
                if (!$list) {
                    $this->error('Please complete the personal data',$this->accountUrl); 
                }else{                    
                    $mentorsID = I('get.mentorsID');
                    $list = M('Mentors')->where($map)->find();
                    if (!$list) {
                        $this->error('The information does not exist');
                    }

                    $list['trainItem'] = trainStrtoArr($list['trainItem']);
                    $where['id'] = array('in',$list['trainItem']);
                    $list['trainItem'] = M('TrainCate')->field('name')->where($where)->select();

                    $this->assign('list',$list);
                    $this->display();
                }                              
            }            
        }else{
            $this->error('Please log in first',U('Login/index')); 
        }
    }

    //拜师
    public function apprentice(){
        $toID = I('post.toID');
        $mentors = M('Mentors')->where('userid='.$toID)->find();
        if (!$mentors) {
            $state = 'error';
            $message = 'The information does not exist';
            echo $this->echo_json_str($state,$message,$url);
            die;
        }
        if ($this->loginStatus) {
            if ($this->userType==2) {
                $state = 'error';
                $message = 'You are the mentors can submit';
            }else{
                //完善资料
                $list = M('Mentees')->where('userid='.$this->userID)->find();
                if (!$list) {
                    $state = 'error';
                    $message = 'Please complete the personal data';
                    $url = $this->accountUrl;
                }else{  

                    $data['fromID']=$this->userID;
                    $data['fromName'] = $list['nickname'];
                    $data['fromFace'] = $list['face'];
                    $data['toID']=$toID;
                    $data['toName']=$mentors['nickname'];
                    $data['toFace']=$mentors['face'];
                    $data['trainItem']=I('post.trainItem');
                    $data['skills']=I('post.skills');
                    $data['target']=I('post.target');
                    $data['train']=I('post.train');
                    $data['work']=I('post.work');
                    $data['createTime'] = time(); 
                    $data['updateTime'] = time(); 
                    $list = M('Apprentice')->add($data);
                    if ($list) {
                        $state = 'SUCCESS';
                        $message = 'Submitted successfully';
                        $url = U('Mentors/detail',array('id'=>$toID));
                    }else{
                        $state = 'error';
                        $message = 'operation failed';
                    }
                }                              
            }            
        }else{
            $state = 'error';
            $message = 'Please log in first';
            $url = U('Login/index');
        }

        echo $this->echo_json_str($state,$message,$url);
    }


    public function email(){
        $mentorsID = I('get.mentorsID');
        $this->assign('mentorsID',$mentorsID);
        $this->display();
    }

    public function saveemail(){
        $toID = I('post.toID');
        $title = I('post.title');
        $content = I('post.content');
        $mentors = M('Mentors')->where('userid='.$toID)->find();
        if (!$mentors) {
            $state = 'error';
            $message = 'The information does not exist';
            echo $this->echo_json_str($state,$message,$url);
            die;
        }
        if ($this->loginStatus) {
            if ($this->userType==2) {
                $state = 'error';
                $message = 'You are the mentors can submit';
            }else{
                //完善资料
                $list = M('Mentees')->where('userid='.$this->userID)->find();
                if (!$list) {
                    $state = 'error';
                    $message = 'Please complete the personal data';
                    $url = $this->accountUrl;
                }else{                    
                    $data['fromID']=$this->userID;
                    $data['fromName'] = $list['nickname'];
                    $data['fromFace'] = $list['face'];
                    $data['toID']=$toID;
                    $data['toName']=$mentors['nickname'];
                    $data['toFace']=$mentors['face'];
                    $data['title']=$title;
                    $data['content']=$content;
                    $data['createTime'] = time(); 
                    $data['type'] = 1; 
                    $list = M('Message')->add($data);
                    if ($list) {
                        $state = 'success';
                        $message = 'Submitted successfully';
                    }else{
                        $state = 'error';
                        $message = 'operation failed';
                    }
                }                              
            }            
        }else{
            $state = 'error';
            $message = 'Please log in first';
            $url = U('Login/index');
        }

        echo $this->echo_json_str($state,$message,$url);
    }


   
}